package com.lucky.controller;

import com.lucky.entity.Person;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @author: Loki
 * @data: 2022-01-24 18:36
 */
@Controller
public class ServletController{

    // 当前Controller数据绑定
    @InitBinder("init")
    private void InitBinder(WebDataBinder binder){
        binder.setFieldDefaultPrefix("a.");
    }
    // 全局数据绑定
    @ModelAttribute(name = "data")
    private Map<String, String> data(){
        Map<String, String> map = new HashMap<>();
        map.put("age","18");
        return map;
    }
    // 全局异常处理
    @ExceptionHandler(Exception.class)
    private Map<String, String> customerException(Exception e){
        Map<String, String> map = new HashMap<>();
        map.put("age","-1");
        return map;
    }

    @RequestMapping("/getCode")
    private void getCode(){
        System.out.println("测试get调用");
    }

    @GetMapping("/index")
    public void index(Person person, Model model){
        System.out.println(person);
    }
}
